package notes.java.concurrent;

import java.util.concurrent.locks.LockSupport;

/** 两个线程交互打印字母和数字 */
public class U17_TestLockSupport3 {

	// 打印字母的线程
	static Thread abThread = null;
	// 打印数字的线程
	static Thread numThread = null;
	
	public static void main(String[] args) {

		// 数据准备
		char[] ab = "ABCDEFGHIJ".toCharArray();
		char[] num = "0123456789".toCharArray();
		
		abThread = new Thread(()->{
			for(char a : ab) {
				System.out.print(a);
				LockSupport.unpark(numThread);// 唤醒打印数字
				LockSupport.park();// 阻塞当前线程
			}
		});
		
		numThread = new Thread(()->{
			for(char n : num) {
				LockSupport.park();// 阻塞当前线程
				System.out.print(n);
				LockSupport.unpark(abThread);// 唤醒打印字母
			}
		});
		
		abThread.start();
		numThread.start();
	}
}